import requestApi from '../requestApi'
import presetVar from '../presetVar';
import {toast} from 'nc-lightapp-front';


export default function searchClick(_props, searchVal, isDefaultSearch) {
    const props = this.props
    const multiLang = props.MutiInit.getIntl(2011);
    if(true){
        let data = {
            querycondition: isDefaultSearch ? (searchVal == null ? {} : searchVal) : _props == null ? {} : _props,
            pagecode: presetVar.pageCode.searchPage,
            pageInfo: {
                pageIndex: -1,
                pageSize: 10,
                totalPage: '0'
            },
            queryAreaCode: presetVar.areaCode.searchArea, //查询区编码
            // oid:'0001Z310000000005J2S',  //查询模板id， oid从木板上取，取默认模板会导致新增的查询条件不生效
            oid: props.meta.getMeta()[presetVar.areaCode.searchArea]['oid'],
            querytype: 'tree',
            tradetype: props.getUrlParam('tradetype') || window.presetVar.tradetype
        };
        let senddata = {data,"pagecode":presetVar.pagecode2,"tepmid":props.meta.getMeta().pageid};
        requestApi.query({
            data:senddata,
            success: (data) => {
                //取消之前的勾选
                const selected_ids = props.transferTable.getTransferTableAllSelectedId() || []
                props.transferTable.cancelRowsChecked(presetVar.areaCode.billsArea, selected_ids.head.head)
                
                //是否是无金额申请
                const noAmount = props.search.getSearchValByField(presetVar.areaCode.searchArea, 'noamount')
                const isNoAmount = noAmount.value && noAmount.value.firstvalue
                //无金额 隐藏金额字段  orig_amount => head.items[] and mtapp_detail.items[]
                const metas = props.meta.getMeta()
                const filterKeys = ['orig_amount', 'pk_currtype', 'rest_amount', 'share_ratio']
                metas.head.items =  isNoAmount ? 
                                    metas.head.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = false} return true}) 
                                    : 
                                    metas.head.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = true} return true}) 
                metas.mtapp_detail.items =  isNoAmount ? 
                                            metas.mtapp_detail.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = false} return true}) 
                                            : 
                                            metas.mtapp_detail.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = true} return true}) 
                props.meta.setMeta(metas)

                if(data){
                    data && props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, data, 'pk_mtapp_bill', 'pk_mtapp_detail');
                }else{
                    props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, [], 'pk_mtapp_bill', 'pk_mtapp_detail');
                }

                isDefaultSearch != 'defaultSearch' && toast({
                    color: 'success',
                    title: multiLang && multiLang.get('201102BCLF-0055'),
                    content: (multiLang && multiLang.get('201102BCLF-0056') || '').replace('{count}', data && data.length || 0)
                })
            }
        })
    }
}

